// JavaScript Document

////////////////////////////////////////////////////////////////////////////
var puploadsrc = BASE_URL+"js/puploadselect.swf";
var puploadurl = BASE_URL+"upload/ds";
var puploadtype = 1;
var puploadftype = "image";//image,video
var puploadparams = [];
var puploadc = 0;
//
var puploadrps = true;
var puploaddeletehandler = function(){};
var puploadcompletehandler = function(){};
//
function puploadinit(ps) {
	if( ps == "item" ) {
		ps = 'name:image;width:1;height:1;type:thumb;';
	} else if( ps == "items" ) {
		ps = 'name:images;width:1;height:1;type:thumb;multi:1;';
	}
	puploadparams = puploadps(ps);
	
	var content = '';
	content += '<div id="PUD" class="pupload">';
	content += '<div id="PUPF">';
	content += puploadf();
	content += '</div>';
	content += '<div id="PUPP"></div>';
	content += '<div id="PUPH">';
	content += puploadh();
	content += '</div>';
	content += '</div>';
	document.write(content);
}
//
function puploadps(ps) {
	var params = [];
	if( ps.indexOf(";") > -1 ) {
		var pss = ps.split(";");
		var pssa = [];
		for(var i=0; i<pss.length; i++) {
			pssa = pss[i].split(":");
			if( pssa[0].length > 0 ) {
				params[pssa[0]] = pssa[1];
			}
		}
	}
	params['field_name'] = 'field_'+params['name'];
	params['field_filename'] = 'field_'+params['name']+'_filename';
	params['field_width'] = 'field_'+params['name']+'_width';
	params['field_height'] = 'field_'+params['name']+'_height';
	
	return params;
}
//
function puploadf() {
	var content = '';
	content += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="PUpload" width="300" height="40" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
	content += '<param name="movie" value="'+puploadsrc+'" />';
	content += '<param name="flashvars" value="upurl='+puploadurl+'?type='+puploadparams['type']+'&uptype='+puploadtype+'&upftype='+puploadftype+'" />';
	content += '<param name="quality" value="high" />';
	content += '<param name="bgcolor" value="#FFFFFF" />';
	content += '<param name="allowScriptAccess" value="sameDomain" />';
	content += '<embed src="'+puploadsrc+'" flashvars="upurl='+puploadurl+'&uptype='+puploadtype+'" quality="high" bgcolor="#FFFFFF" width="300" height="40" name="PUpload" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"></embed>';
	content += '</object>';
	
	return content;
}
//
function puploadh() {
	var content = '';
	var mf = '';
	if( puploadparams['multi'] == 1 ) {
		mf = '[]';
	}
	content += '<input type="hidden" name="'+puploadparams['field_name']+mf+'" id="'+puploadparams['field_name']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_width']+mf+'" id="'+puploadparams['field_width']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_height']+mf+'" id="'+puploadparams['field_height']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_filename']+mf+'" id="'+puploadparams['field_filename']+'_'+puploadc+'">';
	
	return content;
}
//
function puploadreinit() {
	$E("PUPF").innerHTML = puploadf();
	if( puploadparams['multi'] == 1 ) {
		puploadc++;
		$E("PUPH").innerHTML += puploadh();
	}
}
//
function puploadgc(c){
	var gc = [];
	gc.push($E(puploadparams['field_name']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_width']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_height']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_filename']+'_'+puploadc).value);
	
	return gc;
}
//
function puploaddelete(c) {
	if( confirm("Delete?") ) {
		dremove(puploadparams['field_filename']+'_'+c);
		dremove(puploadparams['field_name']+'_'+c);
		dremove(puploadparams['field_width']+'_'+c);
		dremove(puploadparams['field_height']+'_'+c);
		dremove('pup_img_'+c);
		//
		if(puploaddeletehandler){
			puploaddeletehandler(c);
		}
	}
}
////////////////////////////////////////////////////////////////////////////
//
//ps[0]		filename
//ps[1]		filesize
function puploadselect(ps) {
	$E(puploadparams['field_filename']+'_'+puploadc).value = ps[0];
}
//start
function puploadstart() {
	
}
//complete
function puploadcomplete(r) {
	r = r.toString();
	if( r.indexOf(":") > -1 ) {
		var rs = r.split(":");
		if( puploadrps ) {
			$E("PUPP").innerHTML += '<a href="#"><img src="'+BASE_URL+rs[0]+rs[1]+'" id="pup_img_'+puploadc+'" onclick="puploaddelete('+puploadc+')"></a>';
		}
		$E(puploadparams['field_name']+'_'+puploadc).value = rs[1];
		$E(puploadparams['field_width']+'_'+puploadc).value = rs[2];
		$E(puploadparams['field_height']+'_'+puploadc).value = rs[3];
	} else {
		$E(puploadparams['field_name']+'_'+puploadc).value = rs;
	}
	//
	puploadreinit();
	//
	if(puploadcompletehandler){
		puploadcompletehandler();
	}
}
//
//pup_select
//pup_upload
//pup_status
////////////////////////////////////////////////////////////////////////////